package com.kai.steering.data.service.ctl;

import com.kai.steering.data.service.entity.domainObject.ClassificationDO;
import com.kai.steering.data.service.entity.dto.ClassificationQueryDTO;
import com.kai.steering.data.service.entity.po.Classification;
import com.kai.steering.data.service.entity.vo.ClassificationVO;
import com.kai.steering.data.service.service.ClassificationService;
import com.kai.steering.common.vo.Result;
import com.kai.steering.common.web.ctl.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.io.Serializable;
import javax.validation.Valid;

import com.kai.steering.common.vo.Page;
import com.kai.steering.common.web.vo.ReturnCodeExt;

import java.util.Date;
import java.util.List;


/**
 * <pre>
 * 题目分类 前端控制器
 * </pre>
 *
 * @author kai
 * @since 2022-02-23
 */
@Slf4j
@RestController
@RequestMapping("/classification")
@Api(tags = "题目分类 API")
public class ClassificationController extends BaseController {
    @Autowired
    private ClassificationService classificationService;


    @PostMapping
    @ApiOperation(value = "题目分类-添加")
    public Result<Boolean> addClassification(@Valid @RequestBody ClassificationQueryDTO classificationQueryDTO) {

        //参数检查
        if (StringUtils.isEmpty(classificationQueryDTO.getClassificationName())) {
            return Result.failed("参数不完整");
        }
        //构建新对象
        ClassificationDO classificationDO = new ClassificationDO(classificationQueryDTO);
        classificationDO.setClassificationId(null);
        classificationDO.setCreationDatetime(new Date());

        //保存
        boolean flag = classificationService.saveClassification(classificationDO);
        return Result.ok(flag);
    }


    @PutMapping("/{id}")
    @ApiOperation(value = "题目分类-编辑")
    public Result<Boolean> updateClassification(@PathVariable("id") Serializable id, @Valid @RequestBody ClassificationQueryDTO classificationQueryDTO) {

        //参数检查
        if (StringUtils.isEmpty(classificationQueryDTO.getClassificationName())||StringUtils.isEmpty(id.toString())) {
            return Result.failed("参数不完整");
        }
        /**
         * 构建对象
         */
        ClassificationDO classificationDO = new ClassificationDO(classificationQueryDTO);
        classificationDO.setClassificationId(id.toString());

        //修改属性
        boolean flag = classificationService.updateClassification(id,classificationDO);
        return Result.ok(flag);
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "题目分类-删除")
    public Result<Boolean> deleteClassification(@PathVariable("id") String  id) {
        if (StringUtils.isEmpty(id.toString())) {
            return Result.failed("参数不完整");
        }
        boolean flag = classificationService.deleteClassification(id);
        return Result.ok(flag);
    }


    @GetMapping("/{id}")
    @ApiOperation(value = "题目分类-详情")
    public Result<ClassificationVO> getClassification(@PathVariable("id") Serializable id) {
        ClassificationVO classificationQueryVo = new ClassificationVO(classificationService.getClassificationById(id));
        if (classificationQueryVo == null||StringUtils.isEmpty(classificationQueryVo.getClassificationId())) {
            throw ReturnCodeExt.PARAMETER_CHECK.getException("id[" + id + "]对象不存在");
        }

        return Result.ok(classificationQueryVo);
    }


    @PostMapping("/list")
    @ApiOperation(value = "题目分类-综合查询")
    public Result<Page<ClassificationVO>> getClassificationPageList(@Valid @RequestBody ClassificationQueryDTO classificationQueryDTO) {
        Page<ClassificationVO> paging = classificationService.getClassificationPageList(classificationQueryDTO);
        return Result.ok(paging);
    }


    @GetMapping("/list")
    @ApiOperation(value = "题目分类-全部列表")
    public Result<List<ClassificationVO>> getClassificationAllList() {

        List<ClassificationVO> list = classificationService.getAllClassification();
        return Result.ok(list);
    }


}